package com.shinewind.sso.filter;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.shinewind.common.constant.messageConstant;
import com.shinewind.home.service.IModelService;
import com.shinewind.pojo.result.Result;
import com.shinewind.sso.entity.JwtProperties;
import com.shinewind.sso.entity.MyUser;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class MyVerifyCodeFilter extends OncePerRequestFilter {
    @Autowired
    private RedisTemplate redisTemplate;
    @DubboReference
    private IModelService iModelService;
    private MyUser user;

    @Override
    protected void doFilterInternal(HttpServletRequest req, HttpServletResponse resp, FilterChain chain) throws ServletException, IOException {
        //请求路径中时是否包含login关键字，并且请求必须是post
        if(StringUtils.contains(req.getRequestURI(), "/login") && StringUtils.equalsIgnoreCase(req.getMethod(), "post")){
            Boolean flag = checkVerifyCode(req,resp);
            if(flag == true){
                //resp.getWriter().write(new ObjectMapper().writeValueAsString(Result.success(true, messageConstant.VERIFY_SUCCESS)));
                req.setAttribute("user",user);
                chain.doFilter(req,resp);
            }else if(flag == false){
                resp.setContentType("application/json;charset=utf-8");
                resp.getWriter().write(new ObjectMapper().writeValueAsString(Result.fail(false,messageConstant.VERIFY_ERROR)));
                resp.getWriter().flush();
                resp.getWriter().close();
            }
        }
        else{
            chain.doFilter(req,resp);
        }
    }
    //判断验证码
    private Boolean checkVerifyCode(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        user = new ObjectMapper().readValue(req.getInputStream(),MyUser.class);
        System.out.println("CaptCha:"+ user.getVerify());
        System.out.println("redisCaptCha:"+ redisTemplate.opsForValue().get("CaptCha"));

        if(redisTemplate.hasKey("CaptCha") == true){
            if(user.getVerify().equals(redisTemplate.opsForValue().get("CaptCha"))){
                //out.write(new ObjectMapper().writeValueAsString(Result.success(true, messageConstant.VERIFY_SUCCESS)));
                return true;
            }
            else{
                //out.write(new ObjectMapper().writeValueAsString(Result.fail(false,messageConstant.VERIFY_ERROR)));
                return false;
            }
        }
        return null;
    }
}
